package com.inossem.warehousestorage_inossem.adapter.storage;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.inossem.utils.base.RegexUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.storage.StorageManageMatrMoveActivity;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageCheckPositionBean;
import com.inossem.warehousestorage_inossem.bean.storage.StorageManageMatrBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.storage.StorageManageMaterialCheckPositionRequest;
import com.inossem.warehousestorage_inossem.utils.StorageManageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;


/**
 * Created by cjz on 2017/6/6.
 */

public class StorageManageMatrMoveAdapter extends BaseAdapter {

    private Context mContext;

    private StorageManageMatrBean.BodyBean mMaterielItem;

    private List<StorageManageMatrBean.BodyBean.PositionBean> positionList;

    private LayoutInflater inflater;

    private EditText positionEditText, numberEditText;

    private Button add;

    private TextView showAndHide;

    private PercentRelativeLayout hideLayout, secondBlock, specialBlock;

    private TextView specialInventoryDescription, specialInventoryFlag, materielCode, specialInventoryCode, materielDescription, batchCode, inventoryNumber, sendPosition;

    private StorageManageMatrMoveActivity.StorageManageMatrMoveListener mStorageManageMatrMoveListener;

    public StorageManageMatrMoveAdapter(Context context, StorageManageMatrBean.BodyBean materielItem, StorageManageMatrMoveActivity.StorageManageMatrMoveListener storageManageMatrMoveListener) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        mMaterielItem = materielItem;
        positionList = mMaterielItem.getStoragePositionList();
        mStorageManageMatrMoveListener = storageManageMatrMoveListener;
    }

    @Override
    public int getCount() {
        return positionList == null ? 1 : positionList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return positionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.view_storage_manage_matr_move_list_first_item, null);
                specialInventoryDescription = (TextView) convertView.findViewById(R.id.specialInventoryDescription);
                specialInventoryFlag = (TextView) convertView.findViewById(R.id.specialInventoryFlag);
                materielCode = (TextView) convertView.findViewById(R.id.materielCode);
                specialInventoryCode = (TextView) convertView.findViewById(R.id.specialInventoryCode);
                materielDescription = (TextView) convertView.findViewById(R.id.materielDescription);
                batchCode = (TextView) convertView.findViewById(R.id.batchCode);
                inventoryNumber = (TextView) convertView.findViewById(R.id.inventoryNumber);
                sendPosition = (TextView) convertView.findViewById(R.id.sendPosition);
                positionEditText = (EditText) convertView.findViewById(R.id.position);
                numberEditText = (EditText) convertView.findViewById(R.id.number);
                add = (Button) convertView.findViewById(R.id.add);
                hideLayout = (PercentRelativeLayout) convertView.findViewById(R.id.hideLayout);
                secondBlock = (PercentRelativeLayout) convertView.findViewById(R.id.secondBlock);
                specialBlock = (PercentRelativeLayout) convertView.findViewById(R.id.specialBlock);
                showAndHide = (TextView) convertView.findViewById(R.id.showAndHide);
                specialInventoryDescription.setText(mMaterielItem.getSpec_stock_name());
                specialInventoryFlag.setText(mMaterielItem.getSpec_stock());
                materielCode.setText(mMaterielItem.getMat_code());
                specialInventoryCode.setText(mMaterielItem.getSpec_stock_code());
                materielDescription.setText(mMaterielItem.getMat_name());
                batchCode.setText(mMaterielItem.getBatch());
                inventoryNumber.setText(mMaterielItem.getQty());
                sendPosition.setText(mMaterielItem.getS_position_code());
                //隐藏特殊标识等字段
                if (TextUtils.isEmpty(mMaterielItem.getSpec_stock())) {
                    specialBlock.setVisibility(View.GONE);
                } else {
                    specialBlock.setVisibility(View.VISIBLE);
                }
                //展开按钮
                showAndHide.setOnClickListener((View v) -> {
                    if (hideLayout.getVisibility() == View.GONE) {
                        hideLayout.setVisibility(View.VISIBLE);
                        secondBlock.setVisibility(View.VISIBLE);
                        ((TextView) v).setText(mContext.getResources().getString(R.string.view_storage_manage_matr_move_list_first_item_hide_information));
                    } else if (hideLayout.getVisibility() == View.VISIBLE) {
                        hideLayout.setVisibility(View.GONE);
                        secondBlock.setVisibility(View.GONE);
                        ((TextView) v).setText(mContext.getResources().getString(R.string.view_storage_manage_matr_move_list_first_item_show_information));
                    }
                });
                add.setOnClickListener(new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        String checkResult = check(mContext);
                        if (checkResult == null) {
                            addPosition(mContext, positionEditText.getText().toString(), numberEditText.getText().toString(), mStorageManageMatrMoveListener);
                        } else {
                            Utils.middleToast(mContext, checkResult);
                        }

                    }
                });
            }
        } else {
            StorageManageMatrBean.BodyBean.PositionBean positionItem = positionList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_storage_manage_matr_move_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, positionItem);
        }
        return convertView;
    }

    private void addPosition(Context context, String position, String number, StorageManageMatrMoveActivity.StorageManageMatrMoveListener listener) {
        StorageManageMaterialCheckPositionRequest.request(context, position, number, new GsonBeanListener<StorageManageCheckPositionBean>() {
            @Override
            public void onSuccess(StorageManageCheckPositionBean bean) {
                listener.onAddPosition(mMaterielItem.getId(), position, number, mMaterielItem.getUnit_name());
            }

            @Override
            public void onFailing(StorageManageCheckPositionBean bean) {
                Utils.middleToast(context, context.getResources().getString(R.string.activity_storage_manage_matr_move_position_validate_fail));
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(context, bean.getHead().getMsg());
                }
            }
        });
    }

    private String check(Context context) {
        String positionStr = positionEditText.getText().toString();
        String numberStr = numberEditText.getText().toString();

        if (TextUtils.isEmpty(positionStr) || TextUtils.isEmpty(numberStr)) {
            return context.getResources().getString(R.string.activity_storage_manage_matr_move_position_and_number);
        }

        if (StorageManageUtils.hasPosition(mMaterielItem, positionStr)) {
            return context.getResources().getString(R.string.activity_storage_manage_matr_move_position_exist);
        }

        if (!RegexUtils.checkDecimal(numberStr)) {
            return context.getResources().getString(R.string.activity_storage_manage_matr_move_number_illegal);
        }

        if (!Utils.checkRetainDecimal(numberStr, mMaterielItem.getDecimal_place())) {
            return String.format(context.getResources().getString(R.string.common_retain_decimal_illegal), TextUtils.isEmpty(mMaterielItem.getDecimal_place()) ? context.getResources().getString(R.string.common_retain_decimal_unlimited) : mMaterielItem.getDecimal_place());
        }

        if (RegexUtils.checkZero(numberStr)) {
            return context.getResources().getString(R.string.activity_storage_manage_matr_move_number_more_than_zero);
        }

        if (!StorageManageUtils.hasSurplusStock(mMaterielItem, numberStr)) {
            return context.getResources().getString(R.string.activity_storage_manage_matr_move_surplus_stock);
        }

        return null;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.postion = (TextView) view.findViewById(R.id.postion);
        viewHolder.number = (TextView) view.findViewById(R.id.number);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, StorageManageMatrBean.BodyBean.PositionBean positionItem) {
        viewHolder.postion.setText(positionItem.getPosition());
        viewHolder.number.setText(positionItem.getNumber() + positionItem.getUnit());

        //事件处理
        view.setOnLongClickListener((View v) -> {
            DialogUtils.getConfirmDialog(mContext, String.format(mContext.getResources().getString(R.string.activity_storage_manage_delete_position_prompt), viewHolder.postion.getText().toString()), new DialogUtils.ConfirmDialogListener() {
                @Override
                public void onConfirm(Dialog dialog) {
                    dialog.cancel();
                    mStorageManageMatrMoveListener.onDeletePosition(mMaterielItem.getId(),viewHolder.postion.getText().toString());
                }
            }).show();
            return true;
        });
    }

    private class ViewHolder {
        private TextView postion, number;
    }

}
